<?php
class Admin extends Model
{
    public function getCapnhatkm()
    {
        return $this->Db->run("select km_id,km_ten, DATE_FORMAT(km_ngaybd,'%d/%m/%y')as batdau,
          DATE_FORMAT(km_ngaykt,'%d/%m/%y')as ngaykt  from giatrikm UNION select km_id,km_ten, DATE_FORMAT(km_ngaybd,'%d/%m/%y')as batdau,
         DATE_FORMAT(km_ngaykt,'%d/%m/%y')as ngaykt from quatangkm order by km_id ASC ");

    }

    public function getDelete($id)
    {
        $data = $this->Db->select('giatrikm','km_id = :idkm', array('idkm' => $id));
        if(count($data)>0){
            $this->Db->run("delete from chitietgiatrikm where km_id = :idkm", array('idkm' => $id));
            return $this->Db->run("delete from giatrikm where km_id = :idkm", array('idkm' => $id));
        }
        else  {
            $this->Db->run("delete from chitietqt where km_id = :idkm", array('idkm' => $id));
            $this->Db->run("delete from chitietquatangkm where km_id = :idkm", array('idkm' => $id));
            return $this->Db->run("delete from quatangkm where km_id = :idkm", array('idkm' => $id));
        }

    }

    public function getThemkm_quatang()
    {

        $bind = array();
        $bind['idkm'] = $_GET['idkm'];
        $bind['km'] =   $_GET['km'];
        $bind['ngaybd'] = $_GET['ngaybd'];
        $bind['ngaykt'] = $_GET['ngaykt'];

        if(($bind['idkm'] == null) || ($bind['km'] == null) || ($bind['ngaybd'] == null)|| ($bind['ngaykt'] == null)){
            ?>
        <script language="javascript">
            alert('Bạn phải nhập đủ thông tin!');
            //     window.location = "/admin/capnhatkm";
           // App:redirect("/admin/showkm");
        </script>
        <?php   }
        else  {
            ?>
        <script language="javascript">
            alert('Thêm chương trinh khuyến mãi thành công!');
            //     window.location = "/admin/capnhatkm";
       //     App:redirect("/admin/capnhatkm");

        </script>
        <?php
            $sql = "insert into quatangkm(km_id,km_ten,km_ngaybd,km_ngaykt) values (:idkm,:km,:ngaybd,:ngaykt)";
            return $this->Db->run($sql, $bind);

        }
    }


    public function getThemkm_giatri($id)
    {
        $data = $data = $this->Db->select('quatangkm','km_id = :idkm', array('idkm' => $id));
        if(count($data)>0)
        $bind = array();
        $sql = "insert into giatrikm(km_id,km_ten,km_ngaybd,km_ngaykt) values (:idkm,:km,:ngaybd,:ngaykt)";
        $bind['idkm'] = $_GET['idkm'] + 1;
        $bind['km'] =   $_GET['km'];
        $bind['ngaybd'] = $_GET['ngaybd'];
        $bind['ngaykt'] = $_GET['ngaykt'];
        return $this->Db->run($sql, $bind);
    }

    public function getFormsuakm($id)
    {
        $data = $this->Db->select('giatrikm','km_id = :idkm', array('idkm'=>$id));
        if(count($data)>0)
        {
          return $this->Db->run("select km_id,km_ten,km_ngaybd,km_ngaykt from giatrikm where km_id = :idkm", array('idkm' => $id));
        }
        else
        {
          return $this->Db->run("select km_id,km_ten,km_ngaybd,km_ngaykt from quatangkm where km_id = :idkm", array('idkm' => $id));

        }

    }
      public function getSuakm($id)
    {
        $data = $this->Db->select('giatrikm','km_id = :idkm', array('idkm' => $_GET['id']));
        if(count($data)>0){
            $sql = "UPDATE giatrikm SET km_ten= :km, km_ngaybd = :ngaybd,
                  km_ngaykt= :ngaykt where km_id = :idkm";

        }
        else {
            $sql = "UPDATE quatangkm SET km_ten= :km, km_ngaybd = :ngaybd,
                  km_ngaykt= :ngaykt where km_id = :idkm";

        }
        $bind = array();
        $bind['km'] = $_GET['km'];
        $bind['ngaybd'] = $_GET['ngaybd'];
        $bind['ngaykt'] = $_GET['ngaykt'];
        $bind['idkm'] = $_GET['id'];
        return $this->Db->run($sql, $bind);

    }

    public function getQuatangform()
    {
        return $this->Db->run("select * from quatang");
    }

    public function getThemqt()
    {
     /*   $bind = array();
        $sql = "insert into quatang(qt_ten,qt_giatri) values (:qt_ten,:giatri)";
        $bind['qt_ten'] = $_GET['qt_ten'];
        $bind['giatri'] = $_GET['giatri'];
        return $this->Db->run($sql, $bind);*/
        $bind = array();
        $bind['qt_ten'] = $_GET['qt_ten'];
        $bind['giatri'] = $_GET['giatri'];
        if(($bind['qt_ten'] == null) || ($bind['giatri'] == null)){
      ?>
        <script language="javascript">
            alert('Bạn phải nhập đủ thông tin!');
       //     window.location = "/admin/capnhatkm";
            App:redirect("/admin/showkm");
        </script>
        <?php   }
            else  {
        ?>
            <script language="javascript">
                alert('Thêm quà tặng thành công!');
                //     window.location = "/admin/capnhatkm";
                App:redirect("/admin/capnhatkm");

            </script>
        <?php
            $sql = "insert into quatang(qt_ten,qt_giatri) values (:qt_ten,:giatri)";
            return $this->Db->run($sql, $bind);


            }
    }

    public function getXoaqt($id)
    {
        $this->Db->run("delete from chitietqt where qt_id = :qt_id", array('qt_id' => $id));
        return $this->Db->run("delete from quatang where qt_id = :qt_id", array('qt_id' => $id));

    }
    public function getDienthoaikm()
    {
        return $this->Db->run("select c.km_ten, a.dt_ten, b.qt_ten,d.km_id,d.dt_id,d.qt_id from dienthoai as a, quatang as b, quatangkm as c, chitietqt as d
                where a.dt_id = d.dt_id and b.qt_id = d.qt_id and c.km_id = d.km_id");

    }
    public function getGiatrikm()
    {
        return $this->Db->run("select c.km_ten, a.dt_ten, b.ctgtkm_giatri, b.km_id, b.dt_id,b.ctgtkm_giatri from dienthoai as a, chitietgiatrikm as b , giatrikm as c
        where a.dt_id = b.dt_id and c.km_id = b.km_id");

    }

    public function getXoadt_km($id)
    {

        return $this->Db->run("delete from chitietqt where dt_id = :dt_id",
        array('dt_id'=>$id));


    }
    public function getXoactqt($id)
    {
        $this->Db->run("delete from chitietquatangkm where dt_id = :dt_id",array('dt_id'=>$id));
    }

    public function getDsctkm()
    {

        return $this->Db->run("select * from quatangkm");
    }
    public function getDsgiatrikm()
    {

        return $this->Db->run("select * from giatrikm");
    }

    public function getDsdienthoai()
    {
        return $this->Db->run("select * from dienthoai");
    }
    public function getDsquatang()
    {
        return $this->Db->run("select * from quatang");
    }
    public function getThemkm_qt()
    {

        $bind = array();
        $bind['km_id'] = $_GET['ctkm'];
        $bind['dt_id'] = $_GET['dtkm'];
        $bind['qt_id'] = $_GET['qt'];
        $this->Db->run("insert into chitietquatangkm(km_id, dt_id) values (:km_id, :dt_id)", array('km_id'=>$_GET['ctkm'],'dt_id'=>$_GET['dtkm']));
        $sql = "insert into chitietqt(km_id, dt_id, qt_id) values (:km_id, :dt_id, :qt_id)";

        return $this->Db->run($sql,$bind);

    }
   public function getThemkm_gt()
    {

     //  $sql = "insert into chitietgiatrikm(km_id, dt_id, ctgtkm_giatri) values (:km_id, :dt_id, :ctgtkm)";

        $bind = array();
        $bind['km_id'] = $_GET['ctkm'];
        $bind['dt_id'] = $_GET['dtkm'];
        $bind['ctgtkm'] = $_GET['gt'];
     //   return $this->Db->run($sql,$bind);


        if(($bind['ctgtkm'] == null)){
            ?>
        <script language="javascript">
            alert('Bạn phải nhập giá trị khuyến mãi');
            //     window.location = "/admin/capnhatkm";
           // App:redirect("/admin/showkm");
        </script>
        <?php   }
        else  {
            ?>
        <script language="javascript">
            alert('Thêm khuyến mãi giá trị thành công!');
            //     window.location = "/admin/capnhatkm";
          //  App:redirect("/admin/capnhatkm");

        </script>
        <?php
            $sql = "insert into chitietgiatrikm(km_id, dt_id, ctgtkm_giatri) values (:km_id, :dt_id, :ctgtkm)";
            return $this->Db->run($sql, $bind);

        }
    }
    public function getXoagt($id)
    {
        return $this->Db->run("delete from chitietgiatrikm where dt_id = :dt_id",
            array('dt_id'=> $id));
    }





    //showkm
    public function getChiTietkm($id, &$giatri)
    {
        $data = $this->Db->select('giatrikm', 'km_id = :id1', array('id1' => $id));

        #khuyen mai gia tri
        if (count($data) > 0) {
            $giatri = true;
            $sanpham = $this->Db->run("select dt_id, dt_ten, dt_gia, ctgtkm_giatri, (dt_gia-ctgtkm_giatri) as giagiam from chitietgiatrikm ct  NATURAL JOIN dienthoai dt  where km_id= :id ",
                array('id' => $id));
            foreach ($sanpham as &$item) {
                $item['dt_gia_fo'] = App::formatVnd($item['dt_gia']);
                $item['ctgtkm_giatri_fo'] = App::formatVnd($item['ctgtkm_giatri']);
                $item['giagiam_fo'] = App::formatVnd($item['giagiam']);
            }

            App::log($sanpham);
            return $sanpham;
        }
        #Khuyen mai san pham
        else {
            $giatri = false;

            $dienthoai = new Dienthoai();

            $sanpham = $this->Db->run("select * from chitietquatangkm ct NATURAL join dienthoai dt WHERE km_id = :id",
                array('id' => $id));
            # get dienthoai's image url
            foreach ($sanpham as &$item) {
                $item['h_url'] = $dienthoai->getDienthoaiFirstImage($item['dt_id']);
                $item['dt_gia_fo'] = App::formatVnd($item['dt_gia']);

                # get quatang
                $gifts = $this->Db->run('SELECT * FROM chitietqt ct NATURAL JOIN quatang qt WHERE km_id = :km_id ' .
                    'AND dt_id = :dt_id', array('km_id' => $id, 'dt_id' => $item['dt_id']));
                foreach ($gifts as &$gift)
                {
                    $gift['qt_giatri_fo'] = App::formatVnd($gift['qt_giatri']);
                }
                $item['gifts'] = $gifts;


            }

            return $sanpham;
        }
    }

    public function getTenKm($id)
    {
        $data = $this->Db->executeScalar('SELECT km_ten FROM giatrikm WHERE km_id = :id', array('id' => $id));
        $data || $data = $this->Db->executeScalar('SELECT km_ten FROM quatangkm WHERE km_id = :id', array('id' => $id));

        return $data;
    }

}
